package com.system.login.controller;

import com.system.login.dao.GaoyuanLoginsystemMapper;
import com.system.login.dataobject.GaoyuanLoginsystem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 登陆实现
 */

@Controller
public class twiceLoginController {
    @Autowired
    private GaoyuanLoginsystemMapper gaoyuanLoginsystemMapper;

    @RequestMapping(value = "/login/over", produces = "text/plain;charset=UTF-8")
    public ModelAndView overLogin(@RequestParam("name") String name, @RequestParam("pwd") String pwd, @RequestParam("id") String id, HttpSession session, RedirectAttributesModelMap redirectAttributes) {
        session.setAttribute("name", name);
        session.setAttribute("pwd", pwd);
        //System.out.println("1.。。。" + name);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("name", name);

        List<GaoyuanLoginsystem> gaoyuanLoginsystemList = gaoyuanLoginsystemMapper.selectAll(null, null, name, pwd);
        //数据库所有数据写入List

        boolean b = false;
        for (GaoyuanLoginsystem i : gaoyuanLoginsystemList) {
            if (name.equals(i.getName()) && pwd.equals(i.getPwd())) {
                b = true;
                //System.out.println("3...."+i.getName());
            }
        }
        session.setAttribute("name", name);
        session.setAttribute("pwd", pwd);
        redirectAttributes.addAttribute("name", name);

        if (b) {

            return new ModelAndView("redirect:/comments");
        } else {
            return new ModelAndView("redirect:/loginSuccess.html");
        }
    }

    @RequestMapping("/login/out")
    public ModelAndView logout(HttpSession session) {
        session.removeAttribute("name");
        session.removeAttribute("pwd");
        return new ModelAndView("redirect:/loginSuccess.html");
    }

    @RequestMapping(path = "/login/message")
    @ResponseBody
    public String getloginMessage(@SessionAttribute(value = "name", required = false) String name, @SessionAttribute(value = "pwd", required = false) String pwd) {
        if (name == null || pwd == null) {
            return "无登陆数据";
        } else
            return "<h3>登陆用户名是:</h3>" + name + "</br>" + "<h3>登陆密码是:</h3>" + pwd;
    }


    @RequestMapping("/getall/message")
    public String getMapper(Model model) {
        List<GaoyuanLoginsystem> gaoyuanLoginsystemList = gaoyuanLoginsystemMapper.selectAll(null, null, null, null);

        model.addAttribute("users", gaoyuanLoginsystemList);


        return "allmessage";
    }


}
